package mapred.example.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import util.FieldUtil;

public class Page {

    private Url url = new Url();

    private List<String> title = new ArrayList<String>();

    private List<Link> outLinks = new ArrayList<Link>();

    private List<Paragraph> body = new ArrayList<Paragraph>();

    public Url getUrl() {
        return url;
    }

    public List<String> getTitle() {
        return title;
    }

    public List<Link> getOutLinks() {
        return outLinks;
    }

    public List<Paragraph> getBody() {
        return body;
    }

    public void addLink(Link link) {
        if (link != null) {
            this.getOutLinks().add(link);
        }
    }

    public void addParagraph(Paragraph paragraph) {
        if (paragraph != null) {
            this.getBody().add(paragraph);
        }
    }

    public void setTitle(String title) {
        this.getTitle().addAll(Arrays.asList(title.split("\t")));
    }

    public String encodeUrl() {
        return FieldUtil.encode(this.getUrl().getText());
    }

    public String encodeTitle() {
        return FieldUtil.encode(this.getTitle());
    }

    public String encodeBody() {
        if (this.getBody().size() == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (Paragraph para : this.getBody()) {
                sb.append("\1");
                sb.append(para.encode());
            }
            return sb.substring(1);
        }
    }

}
